package com.qf.util;

import java.sql.SQLException;

/**
 * @ClassName TransactionManager
 * @Description TODO
 * @Author 86181
 * @Date 2022/8/31 17:48
 * @Version 1.0
 **/
public class TransactionManager {
    private  ConnectionUtils connectionUtils;

    public void  setConnectionUtils(ConnectionUtils connectionUtils){
        this.connectionUtils=connectionUtils;
    }
    //开启事务
    public  void begin(){
        try {
            System.out.println("开启事务");
            connectionUtils.getConnection().setAutoCommit(false);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    //回滚事务
    public void rollback(){
        try {
            System.out.println("回滚事务");
            connectionUtils.getConnection().rollback();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    //释放资源
    public void release() throws SQLException {
        System.out.println("释放资源");
        connectionUtils.getConnection().close();
    }

    public void commit() {
        try {
            System.out.println("提交事务");
            connectionUtils.getConnection().commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}
